// 享元模式第二种,不需要组件的 data 提供响应式.
// 自己提供响应式 --> 自己提供响应式不行,更新依赖收集无法进行.


const shareObj = {
  shareName: '张三',
  changeName (name) {
    console.log('changeName');
    this.shareName = name
    console.log(this.shareName);
  },
  changeNameAsync (name) {
    console.log('changeNameAsync');
    return new Promise((r, re) => {
      setTimeout(() => {
        this.shareName = name
        console.log(this.shareName);
        r()
      }, 2000);
    })

  }
}

function defineReactive (obj, key, val) {
  if (arguments.length === 2) val = obj[key]
  Object.defineProperty(obj, key, {
    get () {
      console.log('getter');
      return val
    },
    set (newVal) {
      val = newVal
    }
  })
}

function walk (obj) {
  let noFunctionkeys = []
  const allkeys = Object.keys(obj)

  for (let a = 0; a < allkeys.length; a++) {
    if (typeof obj[allkeys[a]] !== 'function') {
      noFunctionkeys = [...noFunctionkeys, allkeys[a]]
    }
  }
  console.log(noFunctionkeys);

  for (let i = 0; i < noFunctionkeys.length; i++) {
    defineReactive(obj, noFunctionkeys[i])
  }
}

walk(shareObj)

export default shareObj
